<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Notebook;
use Dingo\Api\Http\Request;

class NotebookController extends BaseController
{

    public $model = Notebook::class;


    public function index(Request $request) {
        $model = new Notebook();
        $nickname = $request->input('nickname');
        $start_at = $request->input('start_at');
        $end_at = $request->input('end_at');

        $list = $model->when($nickname, function ($q) use($nickname){
            $q->whereHas('user', function ($qr) use($nickname) {
                $qr->where('nickname', 'like', '%'.$nickname.'%');
            });
        })  ->startAt($start_at)
            ->endAt($end_at)->withCertain('cookbook', ['title'])
            ->withCertain('user', ['nickname', 'avatar_url'])
            ->orderBy('id', 'desc')
            ->paginate(\request('per_page', 20));
        return $this->paginate($list);
    }

}

